import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.*;


public class GeneradorClientes {

	/**
	 * @param args
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws IOException, InterruptedException{
		if( args.length!=1){
			System.out.println("Número de parametros incorrectos");
		}
		else{
			int cantClientes = Integer.parseInt(args[0]);
			if (cantClientes<0){
				System.out.println("La cantidad de Clientes no puede ser menor que 0");
			}
			else{
				int puertos[]={4000,4001,4002};
				String [] hosts={"localhost","localhost","localhost"};
				
				DatagramSocket socket=new DatagramSocket();		
				byte[] buffer=new byte[256];
				InetAddress direccionIP=InetAddress.getByName("localhost");
				Integer puerto = 4000;
				String msj= new String ("token");
				buffer=msj.getBytes();
				DatagramPacket pakete=new DatagramPacket(buffer, buffer.length,direccionIP,puerto);
				socket.send(pakete);
				socket.close();
						
				for(int i=1;i<=cantClientes;i++){					
					Cliente cliente = new Cliente(i);
					Random rnd = new Random();
		            Integer server = rnd.nextInt((puertos.length-1)-0+1)+0; // rnd.nextInt(HASTA-DESDE+1)+DESDE
		            cliente.start(hosts[server],puertos[server]);
		            Thread.sleep(3000);
				}
			}
		}
	}

}
